package com.chujian.Activities;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;

import com.chujian.Global.Global;
import com.chujian.Threads.HttpClientPostThread;
import com.chujian.Utils.Constan;
import com.chujian.Utils.Util;
import com.chujian.model.User;
import com.chujian.orderingsystem.R;
import com.google.gson.Gson;

public class UserInfoCompleteActivity extends Activity{
	private EditText mEtUserId;
	private EditText mEtUserPhone;
	private EditText mEtUserName;
	private EditText mEtUserEmail;
	private RadioButton mRbMan;
	private RadioButton mRbFeman;
	private User user;
	private Handler mHandler=new Handler(){
		public void handleMessage(android.os.Message msg) {
			super.handleMessage(msg);
			String str=(String) msg.obj;
			if(str.equals("userUpdateFailed")){
				Util.confirmDialog("提示", "用户信息修改失败！", UserInfoCompleteActivity.this);
				
			}else if(str.equals("userUpdateSuccess")){
				Util.showTextShort(getApplicationContext(), "用户信息修改成功！");
				Global.loginUser.setUserName(user.getUserName());
				Global.loginUser.setUserEmail(user.getUserEmail());
				Global.loginUser.setUserSex(user.getUserSex());
				finish();
			}
			
		};
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_info_complete);
		initView();
		
	}
	
	private void initView() {
		// TODO Auto-generated method stub
		mEtUserId=(EditText) findViewById(R.id.et_user_info_id);
		mEtUserPhone=(EditText) findViewById(R.id.et_user_info_phone);
		mEtUserName=(EditText) findViewById(R.id.et_user_info_name);
		mEtUserEmail=(EditText) findViewById(R.id.et_user_info_email);
		mRbMan=(RadioButton) findViewById(R.id.rb_user_info_man);
		mRbFeman=(RadioButton) findViewById(R.id.rb_user_info_feman);
		if(Global.loginUser==null){
			 AlertDialog.Builder builder = new AlertDialog.Builder(UserInfoCompleteActivity.this);
		        builder.setTitle("警告");
		        builder.setMessage("用户尚未登录");
		        builder.setPositiveButton("确认", new DialogInterface.OnClickListener(){
		            @Override
		            public void onClick(DialogInterface dialog, int which){
		            	Intent intent=new Intent(getApplicationContext(),LoginActivity.class);
		            	startActivity(intent);
		            	finish();
		            }
		        });
		        builder.show();
			
		}else{
			mEtUserId.setText(Global.loginUser.getUserId()+"");
			mEtUserPhone.setText(Global.loginUser.getUserPhone());
			String name=Global.loginUser.getUserName();
			String email=Global.loginUser.getUserEmail();
			String sex=Global.loginUser.getUserSex();
			if(name!=null&&!name.equals("")){
				mEtUserName.setText(Global.loginUser.getUserName()+"");
			}
			if(email!=null&&!email.equals("")){
				mEtUserEmail.setText(Global.loginUser.getUserEmail()+"");
			}
			if(sex!=null&&sex.equals("f")){
				mRbFeman.setChecked(true);
			}else if(sex!=null&&sex.equals("m")){
				mRbMan.setChecked(true);
			}
		}
		
	}
	private void backDialog(){
		 AlertDialog.Builder builder = new AlertDialog.Builder(UserInfoCompleteActivity.this);
	        builder.setTitle("警告！");
	        builder.setMessage("确认退出吗？");
	        builder.setPositiveButton("确认", new DialogInterface.OnClickListener(){
	            @Override
	            public void onClick(DialogInterface dialog, int which){
	            	finish();
	            }
	        });
	        builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){

				@Override
				public void onClick(DialogInterface dialog, int which) {
					
				}
	        });
	        builder.show();
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(keyCode==KeyEvent.KEYCODE_BACK){
			backDialog();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	public void IbBack(View v){
		backDialog();
	}
	public void submit(View v){
		user=new User();
		user.setUserId(Global.loginUser.getUserId());
		user.setUserPhone(Global.loginUser.getUserPhone());
		String userEmail=mEtUserEmail.getText().toString();
		String userName=mEtUserName.getText().toString();
		if(userEmail.equals("")||userName.equals("")){
			Util.confirmDialog("警告！", "请将信息填写完整！", UserInfoCompleteActivity.this);
			return;
		}
		user.setUserEmail(userEmail);
		user.setUserName(userName);
		if(mRbMan.isChecked()){
			user.setUserSex("m");
		}else if(mRbFeman.isChecked()){
			user.setUserSex("f");
		}
		Gson gson =new Gson();
		String jsonString=gson.toJson(user);
		String url=Constan.BASE_URL+"UserInfoCompleteServlet";
		new HttpClientPostThread(url, mHandler, jsonString).start();;
		
		
	}
	
}
